﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using Microsoft.Web.Administration;

namespace CNKI.TPI.Web.Base
{
    public class IISHelper
    {
        public static void CreateSite(string siteName)
        {
            ServerManager iisManager = new ServerManager();
            if (!IsPoolExist(siteName))
            {
                iisManager.ApplicationPools.Add(siteName);
                iisManager.ApplicationPools[siteName].Enable32BitAppOnWin64 = true;
            }
            
            if (!IsSiteExist(siteName))
            {
                string path = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
                iisManager.Sites.Add(siteName, "http", "*:80:" + siteName, path);
                iisManager.Sites[siteName].ApplicationDefaults.ApplicationPoolName = siteName;
            }

            iisManager.CommitChanges();
            return;
        }

        public static bool IsSiteExist(string siteName)
        {
            bool exist = false;
            ServerManager iisManager = new ServerManager();
            if (iisManager.Sites[siteName] != null)
            {
                exist = true;
            }

            return exist;
        }

        public static bool IsPoolExist(string poolName)
        {
            bool exist = false;
            ServerManager iisManager = new ServerManager();
            if (iisManager.ApplicationPools[poolName] != null)
            {
                exist = true;
            }

            return exist;
        }
    }
}
